package cn.itcast.servlet;

import cn.itcast.domain.Student;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 添加学生
 */
@WebServlet("/addStudentServlet")
public class addStudentServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       /* //防止乱码
        req.setCharacterEncoding("utf-8");
        //获取表单的参数，封装为学生对象
        String name = req.getParameter("username");
        String age = req.getParameter("age");
        String score = req.getParameter("score");
        Student student = new Student(name, Integer.parseInt(age), Integer.parseInt(score));
        //写进文件，获取输出流,先防止乱码
        resp.setContentType("text/html;charset=utf-8");
       //获取输出流，指定写出的文件位置
        BufferedWriter bw = new BufferedWriter(new FileWriter("d:\\stu.txt",true));
        bw.write(student.getName()+","+student.getAge()+","+student.getScore());
        bw.newLine();
        bw.close();
        //定时刷新给浏览器
        resp.getWriter().write("添加成功，2秒后跳转到首页");
        resp.setHeader("Refresh","2;URL=/day01/index.html");*/
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
